package tp.badugi.bot.guiViewsBot;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

import tp.badugi.bot.GuiClientWithBot;
import tp.badugi.net.iRequestListener;

public class ActionViewBot extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -594149308479341639L;

	private GuiClientWithBot client;
	
	public final String ACTION_CHECK = "Check";
	public final String ACTION_CALL = "Call";
	public final String ACTION_BET = "Bet";
	public final String ACTION_RAISE = "Raise";
	public final String ACTION_ALLIN = "All in";
	public final String ACTION_FOLD = "Fold";

	private final Dimension actionRequestViewSize = new Dimension(100, 250);
	
	private final Dimension gameViewSize = new Dimension(800, 600);
	
	public JButton actionCheckButton	= new JButton(this.ACTION_CHECK);
	public JButton actionCallButton		= new JButton(this.ACTION_CALL);
	public JButton actionBetButton		= new JButton(this.ACTION_BET);
	public JButton actionRaiseButton	= new JButton(this.ACTION_RAISE);
	public JButton actionAllInButton	= new JButton(this.ACTION_ALLIN);
	public JButton actionFoldButton		= new JButton(this.ACTION_FOLD);
	
	private JButton actions[] = {
			this.actionCheckButton,
			this.actionCallButton,
			this.actionBetButton,
			this.actionRaiseButton,
			this.actionAllInButton,
			this.actionFoldButton,
	};
	
	private JPanel actionPanel = new JPanel();
	
	public JLabel actionTimerLabel = new JLabel();
	
	public Timer actionTimer;
	
	public final int actionTimeout;
	
	public int actionTimeoutCount;
	
	public ActionViewBot(GuiClientWithBot client, String[] possibleActions, int timeout,
			long currentBetDifference, long minimalRaise) {
		
		this.client = client;
		
		this.actionTimeout = timeout/1000;
		
		this.makeView();
		this.refreshView(possibleActions);
	}
	
	private void makeView(){
		
		this.setTitle(this.client.title);
		
		this.actionTimer = new Timer(1000, this.client);
		
		this.setBounds(
				(int)(this.client.windowBoundPoint.getX() + (this.gameViewSize.getWidth()-this.actionRequestViewSize.getWidth())/2),
				(int)(this.client.windowBoundPoint.getY() + (this.gameViewSize.getHeight()-this.actionRequestViewSize.getHeight())/2),
				(int)this.actionRequestViewSize.getWidth(), (int)this.actionRequestViewSize.getHeight());
		
		this.actionPanel.setBackground(Color.BLUE);
		
		this.actionPanel.setLayout(new GridLayout(7,1));
		this.actionPanel.setBorder(new EmptyBorder(30, 10, 30, 10) );
		this.add(this.actionPanel);
		
		this.actionTimerLabel.setBackground(Color.WHITE);
		this.actionTimerLabel.setOpaque(true);
		this.actionTimerLabel.setHorizontalAlignment(SwingConstants.CENTER);
		
		this.actionPanel.add(this.actionTimerLabel);
		this.actionPanel.add(this.actionCheckButton);
		this.actionPanel.add(this.actionCallButton);
		this.actionPanel.add(this.actionBetButton);
		this.actionPanel.add(this.actionRaiseButton);
		this.actionPanel.add(this.actionAllInButton);
		this.actionPanel.add(this.actionFoldButton);
		
		this.actionCheckButton.addActionListener(this.client);
		this.actionCallButton.addActionListener(this.client);
		this.actionBetButton.addActionListener(this.client);
		this.actionRaiseButton.addActionListener(this.client);
		this.actionAllInButton.addActionListener(this.client);
		this.actionFoldButton.addActionListener(this.client);
		
		this.setUndecorated(true);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}
	
	public void refreshView(String[] possibleActions){
		String translatedAction = "";
		
		this.actionTimeoutCount = this.actionTimeout;
		
		this.actionCheckButton	.setEnabled(false);
		this.actionCallButton	.setEnabled(false);
		this.actionBetButton	.setEnabled(false);
		this.actionRaiseButton	.setEnabled(false);
		this.actionAllInButton	.setEnabled(false);
		
		this.actionCheckButton	.setText(iRequestListener.ACTION_CHECK	);
		this.actionCallButton	.setText(iRequestListener.ACTION_CALL	);
		this.actionBetButton	.setText(iRequestListener.ACTION_BET	);
		this.actionRaiseButton	.setText(iRequestListener.ACTION_RAISE	);
		this.actionAllInButton	.setText(iRequestListener.ACTION_ALLIN	);
		this.actionFoldButton	.setText(iRequestListener.ACTION_FOLD	);
		
		for(int i = 0; i < 6; i++){
			translatedAction = this.translateAction(this.actions[i].getText());
			
			for(int j = 0; j < possibleActions.length; j++){
				if(possibleActions[j].equals(this.actions[i].getText())){
					this.actions[i].setEnabled(true);
				}
			}
			this.actions[i].setText(translatedAction);
		}
		
		this.actionTimerLabel.setText(String.valueOf(this.actionTimeout));
		
		this.actionTimer.start();
		
	}
	
	private String translateAction(String action){
		String result;
		
		switch(action){
			case iRequestListener.ACTION_CHECK	: result = this.ACTION_CHECK;
				break;
				
			case iRequestListener.ACTION_CALL	: result = this.ACTION_CALL;
				break;
				
			case iRequestListener.ACTION_BET	: result = this.ACTION_BET;
				break;
				
			case iRequestListener.ACTION_RAISE	: result = this.ACTION_RAISE;
				break;
				
			case iRequestListener.ACTION_ALLIN	: result = this.ACTION_ALLIN;
				break;
				
			case iRequestListener.ACTION_FOLD	: result = this.ACTION_FOLD;
				break;
				
			default : result = "ERROR";
				break;
		}
		
		return result;
	}

}
